package com.dkh.recommend.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dkh.recommend.dto.Result;
import com.dkh.recommend.entity.TbCollect;
import com.dkh.recommend.entity.TbUser;
import com.dkh.recommend.entity.TbUserCollect;
import com.dkh.recommend.mapper.TbCollectMapper;
import com.dkh.recommend.mapper.TbUserCollectMapper;
import com.dkh.recommend.service.ITbUserCollectService;
import com.dkh.recommend.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Transactional
public class TbUserCollectServiceImpl extends ServiceImpl<TbUserCollectMapper, TbUserCollect> implements ITbUserCollectService {

    private TbCollectMapper collectMapper;

//    防止循环依赖注入
    public TbUserCollectServiceImpl(TbCollectMapper tbCollectMapper) {
        this.collectMapper = tbCollectMapper;
    }

    /**
     * 创建收藏夹
     * @param collectName
     * @return
     */
    @Override
    public Result CreateCollect(String collectName) {
//        获取用户id
        Long userId = UserHolder.getUser().getUserId();
//        查看用户是否已经创建该收藏夹
        LambdaQueryWrapper<TbUserCollect> queryWrapper = new LambdaQueryWrapper<>();
       queryWrapper.eq(TbUserCollect::getUserId,userId)
               .eq(TbUserCollect::getCollectName,collectName);
        List<TbUserCollect> list = this.list(queryWrapper);
        if (list.size() > 0){
            return Result.fail("该用户已经存在该收藏夹："+collectName);
        }
//        创建收藏夹
        TbUserCollect tbUserCollect = new TbUserCollect();
        tbUserCollect.setUserId(userId);
        tbUserCollect.setCollectName(collectName);
        this.save(tbUserCollect);
        return Result.ok();
    }

    /**
     * 查看我的收藏夹列表
     * @return
     */
    @Override
    public Result MyCollectList() {
//        获取用户id
        Long userId = UserHolder.getUser().getUserId();
//        根据用户id查找我的收藏夹列表
        LambdaQueryWrapper<TbUserCollect> querryWrapper= new LambdaQueryWrapper<>();
        querryWrapper.eq(TbUserCollect::getUserId,userId);
        List<TbUserCollect> list = this.list(querryWrapper);
        return Result.ok(list);
    }

    /**
     * 修改用户收藏夹名称
     * @param userCollect
     * @return
     */
    @Override
    public Result UpdateCollectName(TbUserCollect userCollect) {
//       1. 先判断该收藏夹是否属于该用户
//        1.1根据收藏夹id获取收藏夹
        TbUserCollect tbUserCollect = this.getById(userCollect.getCollectId());
//        1.2判断该收藏夹创建者是否是本用户
        Long userId = UserHolder.getUser().getUserId();
        if (!tbUserCollect.getUserId().equals(userId) ){
            return Result.fail("该收藏夹不是您的不可以修改");
        }
        userCollect.setUserId(userId);
        this.updateById(userCollect);
        return Result.ok();
    }

    /**
     * 根据收藏夹id删除收藏夹以及收藏夹内的音乐
     * @param collectId
     * @return
     */
    @Override
    @Transactional
    public Result deleteCollect(Long collectId) {
//      获取用户ID
        Long userId = UserHolder.getUser().getUserId();

//        根据收藏夹id删除用户收藏夹的内容
        LambdaQueryWrapper<TbUserCollect> queryWapper = new LambdaQueryWrapper<>();
        queryWapper.eq(TbUserCollect::getCollectId,collectId);
        queryWapper.eq(TbUserCollect::getUserId,userId);
        this.remove(queryWapper);

//        删除收藏夹音乐
        LambdaQueryWrapper<TbCollect> queryWapper2 = new LambdaQueryWrapper<>();
        queryWapper2.eq(TbCollect::getCollectId,collectId);
        collectMapper.delete(queryWapper2);
        return Result.ok();

    }
}
